package com.woniu.apiImpl;

import com.woniu.HisHospitalizationApi.*;
import com.woniu.aggregation.api.NurseServicesQueryApi;
import com.woniu.aggregation.dto.*;
import com.woniu.aggregation.param.*;
import com.woniu.api.HisPatientQueryApi;
import com.woniu.api.HisStaffAPI;
import com.woniu.dto.*;
import com.woniu.dto.PageDTO;
import com.woniu.param.*;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("inpatient-2")
public class NurseServicesQueryApiImpl implements NurseServicesQueryApi {
    //护理表
    @Resource
    private HisNursingApi hisNursingApi;
    //住院记录表
    @Resource
    private HisHospitalRecordsApi hisHospitalRecordsApi;

    //病房表
    @Resource
    private HisRoomApi hisRoomApi;

    //床位表
    @Resource
    private HisBedApi hisBedApi;

    //患者表
    @Resource
    private HisPatientQueryApi hisPatientQueryApi;
    //医嘱表
    @Resource
    private HisOrderFormApi hisOrderFormApi;
    //员工表
    @Resource
    private HisStaffAPI hisStaffAPI;

    @Override
    public Result queryHospitalizationInformation(ListHospitalizationParam listHospitalizationParam) {
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        BeanUtils.copyProperties(listHospitalizationParam,hisHospitalRecordsParam);
        Result<com.woniu.dto.PageDTO<HisHospitalRecordsDto>> hisHospitalRecordsBy = hisHospitalRecordsApi.getHisHospitalRecordsBy(hisHospitalRecordsParam);

        return hisHospitalRecordsBy;
    }

    @Override
    public Result<HospitalizationNewsDto> admissionRegistration(AddHospitalizationParam addHospitalizationParam) {
        //修改患者住院状态
        hisPatientQueryApi.modifyHospitalizationStatus(addHospitalizationParam.getPatientId());

        //住院记录表修改
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        hisHospitalRecordsParam.setHospitalRecordsId(addHospitalizationParam.getHospitalRecordsId());
        hisHospitalRecordsParam.setHospitalStatus(1);
        hisHospitalRecordsParam.setHospitalLeave(new Date());
        Result<HisHospitalRecordsDto> hisHospitalRecordsDtoResult = hisHospitalRecordsApi.updateHisHospitalRecords(hisHospitalRecordsParam);
        //病房表修改
        HisRoomParam hisRoomParam = new HisRoomParam();
        hisRoomParam.setRoomId(addHospitalizationParam.getRoomId());
        Result<com.woniu.dto.PageDTO<HisRoomDto>> hisRoomById = hisRoomApi.getHisRoomById(hisRoomParam);
        //当前病房房间数
        Integer bedNumber = hisRoomById.getData().getList().get(0).getBedFree();
        hisRoomParam.setBedFree(bedNumber-1);
        hisRoomApi.updateHisRoom(hisRoomParam);
        //床位表修改
        HisBedParam hisBedParam = new HisBedParam();
        hisBedParam.setBedId(addHospitalizationParam.getBedId());
        hisBedParam.setBedIsuse(1);
        hisBedApi.updateHisBed(hisBedParam);
        HospitalizationNewsDto hospitalizationNewsDto = new HospitalizationNewsDto();
        BeanUtils.copyProperties(hisHospitalRecordsDtoResult.getData(),hospitalizationNewsDto);
        return Result.bulid(hospitalizationNewsDto);
    }

    @Override
    public Result<InpatientInformationDto> nursesQueryPatientInformation(Integer patientId) {
        //住院记录表
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        hisHospitalRecordsParam.setPatientId(patientId);
        hisHospitalRecordsParam.setHospitalStatus(1);
        Result<com.woniu.dto.PageDTO<HisHospitalRecordsDto>> hisHospitalRecordsBy = hisHospitalRecordsApi.getHisHospitalRecordsBy(hisHospitalRecordsParam);
        //患者信息表
        ListHisPatientParam listHisPatientParam = new ListHisPatientParam();
        listHisPatientParam.setPatientId(patientId);
        Result<com.woniu.dto.PageDTO<HisPatientDto>> pageDTOResult = hisPatientQueryApi.queryPatientInformation(listHisPatientParam);
        InpatientInformationDto inpatientInformationDto = new InpatientInformationDto();
        BeanUtils.copyProperties(hisHospitalRecordsBy.getData().getList().get(0),inpatientInformationDto);
        BeanUtils.copyProperties(pageDTOResult.getData().getList().get(0),inpatientInformationDto);
        return Result.bulid(inpatientInformationDto);
    }

    @Override
    public Result queryBed(BedInformationParam bedInformationParam) {
        HisBedParam hisBedParam = new HisBedParam();
        BeanUtils.copyProperties(bedInformationParam,hisBedParam);
        return hisBedApi.getHisBedBy(hisBedParam);
    }

    @Override
    public Result wardInformation(WardInformationParam wardInformationParam) {
        HisRoomParam hisRoomParam = new HisRoomParam();
        BeanUtils.copyProperties(wardInformationParam,hisRoomParam);
        return hisRoomApi.getHisRoomById(hisRoomParam);
    }

//    @Override
//    public Result<PageDTO<WardBedInformationDto>> WardBedInformation(WardBedInformationParam wardBedInformationParam) {
//        return null;
//    }

    @Override
    public Result ModifyBedsAndWards(BedOrTransferParam bedOrTransferParam) {
        //根据患者ID查询患者的住院表主键ID
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        hisHospitalRecordsParam.setPatientId(bedOrTransferParam.getPatientId());
        Result<PageDTO<HisHospitalRecordsDto>> hisHospitalRecordsBy = hisHospitalRecordsApi.getHisHospitalRecordsBy(hisHospitalRecordsParam);

        HisHospitalRecordsParam hisHospitalRecordsParam1 = new HisHospitalRecordsParam();

        hisHospitalRecordsParam1.setHospitalRecordsId(hisHospitalRecordsBy.getData().getList().get(0).getHospitalRecordsId());
        if(bedOrTransferParam.getDoctorId()!=null){//转科
            //查询医生信息
            QueryStaffParam queryStaffParam = new QueryStaffParam();
            QueryCriteriaStaffParam queryCriteriaStaffParam = new QueryCriteriaStaffParam();
            queryCriteriaStaffParam.setStaffId(bedOrTransferParam.getDoctorId());
            queryStaffParam.setQueryCriteriaStaffParam(queryCriteriaStaffParam);
            Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);

            StaffDto staffDto = queryStaffDtoResult.getData().getStaffDtoList().getList().get(0);
            hisHospitalRecordsParam1.setDoctorName(staffDto.getStaffName());
            hisHospitalRecordsParam1.setOfficeId(staffDto.getOfficeId());

        }else{//转床
            HisBedParam hisBedParam = new HisBedParam();
            hisBedParam.setBedId(bedOrTransferParam.getBedId());
            hisBedParam.setBedIsuse(0);
            hisBedApi.updateHisBed(hisBedParam);
            hisBedParam.setBedId(bedOrTransferParam.getNewBedId());
            hisBedParam.setBedIsuse(1);
            Result<HisBedDto> hisBedDtoResult = hisBedApi.updateHisBed(hisBedParam);
            hisHospitalRecordsParam1.setBedId(hisBedDtoResult.getData().getBedId());
            hisHospitalRecordsParam1.setBedNum(hisBedDtoResult.getData().getBedNum());

        }

        if(bedOrTransferParam.getRoomId()!=bedOrTransferParam.getNewRoomId()){//房间改变
            HisRoomParam hisRoomParam = new HisRoomParam();
            hisRoomParam.setRoomId(bedOrTransferParam.getRoomId());
            Result<PageDTO<HisRoomDto>> hisRoomById = hisRoomApi.getHisRoomById(hisRoomParam);
            Integer bedFree = hisRoomById.getData().getList().get(0).getBedFree();
            bedFree=bedFree+1;
            hisRoomParam.setBedFree(bedFree);
            hisRoomApi.updateHisRoom(hisRoomParam);

            hisRoomParam.setRoomId(bedOrTransferParam.getNewRoomId());
            Result<PageDTO<HisRoomDto>> hisRoomById1 = hisRoomApi.getHisRoomById(hisRoomParam);
            Integer bedFree1 = hisRoomById1.getData().getList().get(0).getBedFree();
            bedFree1=bedFree1-1;
            hisRoomParam.setBedFree(bedFree1);
            Result<HisRoomDto> hisRoomDtoResult = hisRoomApi.updateHisRoom(hisRoomParam);
            hisHospitalRecordsParam1.setRoomId(hisRoomDtoResult.getData().getRoomId());
            hisHospitalRecordsParam1.setRoomNumber(hisRoomDtoResult.getData().getRoomNumber());
            hisHospitalRecordsParam1.setOfficeName(hisRoomDtoResult.getData().getOfficeName());
        }
        //修改完后在修改住院记录表



        return hisHospitalRecordsApi.updateHisHospitalRecords(hisHospitalRecordsParam1);
    }

    @Override
    public Result newCare(AddNursingParam addNursingParam) {
        HisNursingAddParam hisNursingAddParam = new HisNursingAddParam();
        BeanUtils.copyProperties(addNursingParam,hisNursingAddParam);
        Result<HisNursingDto> hisNursingDtoResult = hisNursingApi.addHisNursing(hisNursingAddParam);
        NurseDto nurseDto = new NurseDto();
        BeanUtils.copyProperties(hisNursingDtoResult.getData(),nurseDto);

        return Result.bulid(nurseDto);

    }

    @Override
    public Result inquiryNursing(ListNurseingParam listNurseingParam) {
        HisNursingParam hisNursingParam = new HisNursingParam();
        BeanUtils.copyProperties(listNurseingParam,hisNursingParam);
        Result<com.woniu.dto.PageDTO<HisNursingDto>> hisNursingBy = hisNursingApi.getHisNursingBy(hisNursingParam);

        return Result.bulid(hisNursingBy);
    }

    @Override
    public Result nursesQueryMedicalOrders(ListOrdersParam listOrdersParam) {
        HisOrderFormParam hisOrderFormParam = new HisOrderFormParam();
        BeanUtils.copyProperties(listOrdersParam,hisOrderFormParam);
        Result<com.woniu.dto.PageDTO<com.woniu.dto.HisOrderFormDto>> allHisOrderFormBy = hisOrderFormApi.getAllHisOrderFormBy(hisOrderFormParam);
        return allHisOrderFormBy;
    }

    @Override
    public Result modifyOrderStatus(ModifyOrderStatusParam modifyOrderStatusParam) {
        HisOrderFormParam hisOrderFormParam = new HisOrderFormParam();
        BeanUtils.copyProperties(modifyOrderStatusParam,hisOrderFormParam);
        hisOrderFormParam.setOrderFormTime(new Date());
        Result<com.woniu.dto.HisOrderFormDto> hisOrderFormDtoResult = hisOrderFormApi.updateHisOrderForm(hisOrderFormParam);
        return hisOrderFormDtoResult;
    }

    @Override
    public Result leaveHospital(ModifyHospitalParam modifyHospitalParam) {
        //修改住院状态
        hisPatientQueryApi.modifyHospitalizationStatus(modifyHospitalParam.getPatientId());
        //修改患者住院表记录
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        hisHospitalRecordsParam.setHospitalRecordsId(modifyHospitalParam.getHospitalRecordsId());
        hisHospitalRecordsParam.setHospitalStatus(2);
        hisHospitalRecordsParam.setHospitalLeave(new Date());
        hisHospitalRecordsApi.updateHisHospitalRecords(hisHospitalRecordsParam);

        //更改床位状态
        HisBedParam hisBedParam = new HisBedParam();
        hisBedParam.setBedId(modifyHospitalParam.getBedId());
        hisBedParam.setBedIsuse(0);
        hisBedApi.updateHisBed(hisBedParam);
        //更改房间床位数量
        HisRoomParam hisRoomParam = new HisRoomParam();
        hisBedParam.setRoomId(modifyHospitalParam.getRoomId());
        Integer bedFree = hisRoomApi.getHisRoomById(hisRoomParam).getData().getList().get(0).getBedFree();
        hisRoomParam.setBedFree(bedFree);
        hisRoomApi.updateHisRoom(hisRoomParam);

        return Result.bulid().setFlag(true).setMessage("成功").setCode(200);
    }
}
